import numpy as np
import matplotlib.pyplot as plt

gset_path = 'G39.txt'
gset_result_path = 'G22testresult6convergence350.txt'
test = 2

if test == 1:
    with open(gset_path, mode='r') as f:
        title = f.readline()
        if title == 'ROWS':
            print("It is eCut problem.")
            # TODO: process the eCut problem.
            exit(0)
        else:
            vertex, edge = title.split()
            vertex = int(vertex)
            edge = int(edge)
            print(vertex)
            weight = np.zeros((vertex, vertex))
            print(weight.shape)
            edge_relationship = f.readlines()
            for cnt in range(edge):
                edge_a_str, edge_b_str, weight_a_b_str = edge_relationship[cnt].split()
                edge_a = int(edge_a_str)
                edge_b = int(edge_b_str)
                weight_a_b = int(weight_a_b_str)
                weight[edge_a - 1][edge_b - 1] = weight_a_b
                weight[edge_b - 1][edge_a - 1] = weight_a_b

            print(weight)
            print(edge)
            print(weight.sum())
            X = np.ones((vertex, 1))
            energe = X.T @ weight @ X
            print(energe[0][0])

if test == 2:
    with open(gset_result_path, mode='r') as f:
        result_time = []
        result_value = []
        result = f.readlines()
        for i in range(100):
            _, time_line, __ = result[i * 4 + 3].split()
            result_time.append(float(time_line))
            _, __, value_line = result[i * 4 + 2].split()
            result_value.append(int(value_line))
    plt.figure(1)
    plt.plot(result_time, result_value, 'k.')
    plt.figure(2)
    plt.hist(result_time, edgecolor='k')
    plt.figure(3)
    plt.hist(result_value, edgecolor='k')
    print(max(result_time))
    print(min(result_time))
    print(max(result_value))
    print(min(result_value))
    plt.show()

    input()

